﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Timers;
using System.IO.Ports;
using CIRDiagnoseDevice.Test;
using CIRDiagnoseDevice.DBManage;
using System.Collections;
using CIRDiagnoseDevice.Control;
using CIRDiagnoseDevice.TestConfig;

namespace CIRDiagnoseDevice
{
    public partial class SingleModFrm : Form
    {
        #  region 全局变量
        public string TestTime, Tester, Dev_num; // 保存测试时间、测试员、CIR编号
        private _450MFunTrial TrainEndFunTrial;
        private CIR_450M_CoRecModTest CoRecTest;
        private CIR_450M_PiRecModTest PiRecTest;
        private CIR_450M_SigModTest SigTest;
        private CIR_450M_TranModTest TranTest;
        private CIR_HeLuQi_Test heluqiTest;//合路器测试类
        private GSM_RDigModTest GsmDigTest;
        private GSM_RFunTrial GsmFunTrial;
        public GSM_RVoModTest GsmVoTest;
        private LBJFunTrial LbjFunTrial;
        private _450MDispatchCommTrial dispatchCommTrial;
        private CheckBox HeaderCheckBox;
        private Rectangle oRectangle;
        private TrainApproachEarlyWarning TrainApproach;
        private LbjTranTest LbjTranTest;
        private LbjRecTest LbjRecTest;
        public string ToolStrip;
        public static System.Data.DataTable dt;
        public SetToolStripStatusInfo SetToolStripStatusInfo; 
        #  endregion

        public SingleModFrm()
        {
            InitializeComponent();
        }

        public SingleModFrm(string formText)
        {
            InitializeComponent();
            this.Text = formText;
        }

        public static DataTable DtTable
        {
            get
            {
                return dt;
            }
        }

        private void SingleModFrm_Load(object sender, EventArgs e)
        {

        }

        #   region 设置CheckBox不可选/可选
        public void SetCheckBoxUnEdit(bool unEdit)
        {
            if (dataGridView1.InvokeRequired)
            {
                dataGridView1.Invoke(new MethodInvoker(delegate
                {
                    dataGridView1.Columns[0].ReadOnly = unEdit;
                }));
            }
            else
            {
                dataGridView1.Columns[0].ReadOnly = unEdit;
            }
        }
        #   endregion



        #　region 开始测试各模块
        /// <summary>
        /// 开始450MHz调度命令功能试验
        /// </summary>
        public void Start450MDispatchTrial(SerialPort OSP, ref Thread dispatchThread)
        {
            if (dispatchCommTrial == null)
            {
                dispatchCommTrial = new _450MDispatchCommTrial(dataGridView1, 0, null);
            }
            dispatchCommTrial.InitParameter(OSP);
            dispatchCommTrial.Start450MDispatch(ref dispatchThread);
            MainFrm.MotherFrm._450MDispatchFunTrial = true;
        }

        /// <summary>
        /// GSM-R语音模块单板测试
        /// </summary>
        /// <param name="OSP"></param>
        /// <param name="TestGSM_RVoiThread"></param>
        /// <param name="queue"></param>
        public void StartGsmVoModTest(SerialPort OSP, ref Thread TestGSM_RVoiThread, Queue queue, SerialPort GSM_RModPort)
        {
            if (MessageBox.Show("现在测试GSM-R语音单板模块，请将4202测试仪表连接到GSM-R语音单板模块！", "用户操作提示", MessageBoxButtons.OK) == DialogResult.OK)
            {
                if (GsmVoTest == null)
                {
                    GsmVoTest = new GSM_RVoModTest(this, dataGridView1, 0, OSP, queue, GSM_RModPort);
                    GsmVoTest.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
                }
                MainFrm.MotherFrm.IsGsmModTest = true;
                MainFrm.MotherFrm.CallSuccessed = false;
                GsmVoTest.InitParameter();
                InitDataGridView(false);
                TestGSM_RVoiThread = new Thread(new ThreadStart(GsmVoTest.Test_Gsm_rVoMod));
                TestGSM_RVoiThread.IsBackground = true;
                TestGSM_RVoiThread.Start();
            }
        }

        /// <summary>
        /// GSM-R数据模块单板测试
        /// </summary>
        /// <param name="TestGSM_RDigThread"></param>
        /// <param name="OSP"></param>
        /// <param name="queue"></param>
        public void StartGsmDigModTest(ref Thread TestGSM_RDigThread, SerialPort OSP, Queue queue)
        {
            if (GsmDigTest == null)
            {
                GsmDigTest = new GSM_RDigModTest(this, dataGridView1, 0, OSP, queue, null);
                GsmDigTest.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
            }
            GsmDigTest.InitParameter();
            MainFrm.MotherFrm.IsGsmModTest = true;
            InitDataGridView(false);
            TestGSM_RDigThread = new Thread(new ThreadStart(GsmDigTest.TestGSM_RDigMod));
            TestGSM_RDigThread.IsBackground = true;
            TestGSM_RDigThread.Start();
            string str = "开始测试GSM_R数据模块";
            MainFrm.MotherFrm.SetToolStrip(str);
        }

        /// <summary>
        /// 列车接近预警
        /// </summary>
        /// <param name="OSP"></param>
        public void SartTrainApproachFunTrial(SerialPort OSP, ref Thread TrainApproachThread)
        {
            if (TrainApproach == null)
            {
                TrainApproach = new TrainApproachEarlyWarning(OSP, dataGridView1, 0);
            }
            InitDataGridView(false);
            TrainApproach.StartTrainApproachTrial(ref TrainApproachThread);
        }

        /// <summary>
        /// 450M列尾功能试验
        /// </summary>
        public void Start450MFunTrial(SerialPort OSP, ref Thread trainEndThread)
        {
            if (TrainEndFunTrial == null)
            {
                TrainEndFunTrial = new _450MFunTrial(this, dataGridView1, 0, OSP, null);
                TrainEndFunTrial.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
            }
            InitDataGridView(true);
            TrainEndFunTrial.InitParameter();
            if (trainEndThread != null)
            {
                trainEndThread.Abort();
            }
            trainEndThread = new Thread(new ThreadStart(TrainEndFunTrial.DealWithReceiveData));
            trainEndThread.IsBackground = true;
            trainEndThread.Start();
        }

        /// <summary>
        /// LBJ功能试验
        /// </summary>
        public void StartLbjFunTrial(SerialPort OSP, ref Thread lbjThread)
        {
            if (LbjFunTrial == null)
            {
                LbjFunTrial = new LBJFunTrial(this, dataGridView1, 0, OSP);
                LbjFunTrial.SetToolStripStatusInfo = SetToolStripStatusInfo;
                LbjFunTrial.OnMessageNote += new ThreadEndNotice(MainFrm.MotherFrm.OnMessageNotice);

            }
            LbjFunTrial.InitParameter();//初始化了接受数据
            InitDataGridView(true);
            lbjThread = new Thread(new ThreadStart(LbjFunTrial.DealWithData));
            lbjThread.IsBackground = true;
            lbjThread.Start();
            ToolStrip = LbjFunTrial.ToolStripInfosx;
        }

        /// <summary>
        /// LBJ发射机性能指标测试
        /// </summary>
        /// <param name="OSP">与2945B通信串口</param>
        /// <param name="comm">与Lbj通信串口</param>
        /// <param name="lbjTranThread">测试Lbj发射机线程</param>
        /// <param name="queue" > 接收2945B仪表数据队列</param>
        public void StartLbjTranTest(SerialPort OSP, SerialPort comm, ref Thread lbjTranThread, Queue queue)
        {
            if (MessageBox.Show("现在测试LBJ发射机性能指标，请将测试仪表连接到LBJ上,\r\n并将接口单元的LBJ接口与LBJ的维护口连接", "提示", MessageBoxButtons.OK) == DialogResult.OK)
            {
                if (LbjTranTest == null)
                {
                    LbjTranTest = new LbjTranTest(OSP, 0, null, queue, comm);
                    LbjTranTest.SetToolStripStatusInfo = SetToolStripStatusInfo;
                    LbjTranTest.OnMessageNote += new ThreadEndNotice(MainFrm.MotherFrm.OnMessageNotice);
                }
                InitDataGridView(false);
                MainFrm.MotherFrm.TestLbjTran = true;
                lbjTranThread = new Thread(new ParameterizedThreadStart(LbjTranTest.TestLbjTransmitter));
                lbjTranThread.IsBackground = true;
                lbjTranThread.Start(dataGridView1);
                SetCheckBoxUnEdit(true);
            }
        }

        /// <summary>
        /// LBJ接收机性能指标测试
        /// </summary>
        /// <param name="OSP">与2945B通信串口</param>
        /// <param name="comm">与Lbj通信串口</param>
        /// <param name="lbjTranThread">测试Lbj发射机线程</param>
        /// <param name="queue" > 接收2945B仪表数据队列</param>
        public void StartLbjRecTest(SerialPort OSP, SerialPort comm, ref Thread lbjRecThread, Queue queue)
        {
            if (MessageBox.Show("现在测试LBJ接收机性能指标，请将测试仪表连接到LBJ上,\r\n并将接口单元的LBJ接口与LBJ的维护口连接", "提示", MessageBoxButtons.OK) == DialogResult.OK)
            {
                if (LbjRecTest == null)
                {
                    LbjRecTest = new LbjRecTest(OSP, comm, 0, null, queue);
                    LbjRecTest.OnMessageNote += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
                }
                MainFrm.MotherFrm.TestLbjRec = true;
                InitDataGridView(false);
                lbjRecThread = new Thread(new ParameterizedThreadStart(LbjRecTest.LbjRecNorTest));
                lbjRecThread.IsBackground = true;
                lbjRecThread.Start(dataGridView1);
                SetCheckBoxUnEdit(true);
            }
        }

        /// <summary>
        /// 测试GSM-R语音模块
        /// </summary>
        public void StartGsmVoTest(SerialPort OSP, ref Thread TestGSM_RVoiThread, Queue queue, SerialPort GSM_RModPort)
        {
            if (MessageBox.Show("现在测试GSM-R语音模块，请选择GSM-R模式运行线路，\r\n\r\n并将4202测试仪表连接到CIR的语音模块且连接好送受话器！", "提示", MessageBoxButtons.OK) == DialogResult.OK)
            {
                if (GsmVoTest == null)
                {
                    GsmVoTest = new GSM_RVoModTest(this, dataGridView1, 0, OSP, queue, GSM_RModPort);
                    GsmVoTest.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
                }
                MainFrm.MotherFrm.IsGsmModTest = false;
                GsmVoTest.InitParameter();
                InitDataGridView(false);
                TestGSM_RVoiThread = new Thread(new ThreadStart(GsmVoTest.Test_Gsm_rVoMod));
                TestGSM_RVoiThread.IsBackground = true;
                TestGSM_RVoiThread.Start();
            }
        }

        /// <summary>
        /// 测试GSM-R数据模块
        /// </summary>
        public void StartGsmDigTest(ref Thread TestGSM_RDigThread, SerialPort OSP, Queue queue)
        {
            if (GsmDigTest == null)
            {
                GsmDigTest = new GSM_RDigModTest(this, dataGridView1, 0, OSP, queue, null);
                GsmDigTest.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
            }
            GsmDigTest.InitParameter();
            InitDataGridView(false);
            TestGSM_RDigThread = new Thread(new ThreadStart(GsmDigTest.TestGSM_RDigMod));
            TestGSM_RDigThread.IsBackground = true;
            TestGSM_RDigThread.Start();
            string str = "开始测试GSM_R数据模块";
            MainFrm.MotherFrm.SetToolStrip(str);
        }

        /// <summary>
        /// GSM-R数据模块功能试验
        /// </summary>
        public void StartGsmFunTrial(SerialPort OSP, SerialPort OSP2, ref Thread FunTrialThread)
        {
            if (GsmFunTrial == null)
            {
                GsmFunTrial = new GSM_RFunTrial(this, dataGridView1, 0, OSP, OSP2);
                GsmFunTrial.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
            }
            InitDataGridView(true);
            GsmFunTrial.StartGsmFunTrial();
            FunTrialThread = new Thread(new ThreadStart(GsmFunTrial.DealWithData));
            FunTrialThread.IsBackground = true;
            FunTrialThread.Start();
        }

        /// <summary>
        /// 测试450M同频接收机
        /// </summary>
        public void StartCIR_450M_CoRecModTest(ref Thread Test450M_CoRecThread, SerialPort OSP, Queue queue)
        {
            if (CoRecTest == null)
            {
                CoRecTest = new CIR_450M_CoRecModTest(this, 0, null, dataGridView1, OSP, queue);
                CoRecTest.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
            }
            CoRecTest.InitParameter();
            InitDataGridView(false);
            Test450M_CoRecThread = new Thread(new ThreadStart(CoRecTest.TestCo_Reciever));
            Test450M_CoRecThread.IsBackground = true;
            Test450M_CoRecThread.Start();
            SetCheckBoxUnEdit(true);
        }

        /// <summary>
        /// 测试450M异频接收机
        /// </summary>
        public void StartCIR_450M_PiRecModTest(ref Thread Test450M_PiRecThread, SerialPort OSP, Queue queue)
        {
            if (PiRecTest == null)
            {
                PiRecTest = new CIR_450M_PiRecModTest(this, 0, null, OSP, queue);
                PiRecTest.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
            }
            PiRecTest.InitParameter();
            InitDataGridView(false);
            Test450M_PiRecThread = new Thread(new ParameterizedThreadStart(PiRecTest.TestPilot_Reciever));
            Test450M_PiRecThread.IsBackground = true;
            Test450M_PiRecThread.Start(dataGridView1);
            SetCheckBoxUnEdit(true);
        }

        /// <summary>
        /// 测试450M信令指标
        /// </summary>
        public void StartCIR_450M_SigModTest(ref Thread Test450M_SigThread, SerialPort OSP, Queue queue)
        {
            if (SigTest == null)
            {
                SigTest = new CIR_450M_SigModTest(this, 0, null, OSP, queue);
            }
            SigTest.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
            SigTest.InitParameter();
            InitDataGridView(false);
            Test450M_SigThread = new Thread(new ParameterizedThreadStart(SigTest.TestSigStandard));
            Test450M_SigThread.IsBackground = true;
            Test450M_SigThread.Start(dataGridView1);
            SetCheckBoxUnEdit(true);
        }

        /// <summary>
        /// 450M发射机指标测试
        /// </summary>
        public void StartCIR_450M_TranModTest(ref Thread Test450M_TranThread, SerialPort OSP, Queue queue)
        {

            if (TranTest == null)
            {
                TranTest = new CIR_450M_TranModTest(this, 0, null, OSP, queue);
                TranTest.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
            }

            Test450M_TranThread = new Thread(new ParameterizedThreadStart(TranTest.TestTrismitter));
            TranTest.InitParameter();
            InitDataGridView(false);
            Test450M_TranThread.IsBackground = true;
            Test450M_TranThread.Start(dataGridView1);
            SetCheckBoxUnEdit(true);
        }
        /// <summary>
        /// 合路器测试
        /// </summary>
        public void StartHeLuQiTest(ref Thread heluqi_Thread, SerialPort LbjNorSerialPort, SerialPort GSM_RModPort, Queue queue)
        {

            if (heluqiTest == null)
            {
                heluqiTest = new CIR_HeLuQi_Test(this, 0, null,  queue, LbjNorSerialPort, GSM_RModPort);
                heluqiTest.SetToolStripStatusInfo = SetToolStripStatusInfo;
                heluqiTest.OnMessageNotice += new MessageNotice(MainFrm.MotherFrm.OnMessageNotice);
            }
            MainFrm.MotherFrm.IsGsmModTest = false;
            heluqi_Thread = new Thread(new ParameterizedThreadStart(heluqiTest.TestTrismitter));
            heluqiTest.InitParameter();
            InitDataGridView(false);
            heluqi_Thread.IsBackground = true;
            heluqi_Thread.Start(dataGridView1);
            SetCheckBoxUnEdit(true);
        }
        # endregion

        /// <summary>
        /// 初始化450M指标测试表格
        /// </summary>
        public void InitGrid(string groupid)
        {
            dt = new System.Data.DataTable();
           
            //DataColumn col1 = new DataColumn("测试项目");
            //DataColumn col6 = new DataColumn("单位");
            //DataColumn col2 = new DataColumn("指标要求");
            //DataColumn col3 = new DataColumn("测试值");
            //DataColumn col4 = new DataColumn("测试结果");//,System.Type.GetType("System.Byte[]"));
            //DataColumn col = new DataColumn("全选");
            //col.DataType = typeof(Boolean);
            //dt.Columns.Add(col);
            //dt.Columns.Add(col1);
            //dt.Columns.Add(col6);
            //dt.Columns.Add(col2);
            //dt.Columns.Add(col3);
            //dt.Columns.Add(col4);

            TestRecordManage trm = new TestRecordManage();
            trm.OpenDataBase();

            dt.Columns.AddRange(new DataColumn[] { new DataColumn("全选", typeof(Boolean)) });
            dt.Columns[0].DefaultValue = true;
            StringBuilder sb = new StringBuilder();
            sb.Append("select distinct SUBSTRING(TestName, 1, case  when charindex('#',TestName )=0 then len(TestName) ");
            sb.Append(" else charindex('#',TestName )-1 end ) AS N'测试项目',unit AS N'单位',remark AS N'指标要求','' AS N'测试值',''AS N'测试结果', ");
            sb.Append("ttic.TestGroupID,ttic.TestID from tb_TestIndexConfig ttic where ttic.TestGroupID in (" + groupid + ") order by  ttic.TestGroupID,ttic.TestID");
            dt.Merge(trm.ExecuteSqlTable(sb.ToString()));
            dt.Columns.Remove(dt.Columns["TestGroupID"]);
            dt.Columns.Remove(dt.Columns["TestID"]); 
            trm.CloseDataBase();

            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].FillWeight = 45;
            dataGridView1.Columns[1].FillWeight = 220;
            dataGridView1.Columns[0].ValueType = typeof(bool);
            dataGridView1.Columns[0].ReadOnly = false;
            for (int i = 1; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].ReadOnly = true;
            }
            dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[5].SortMode = DataGridViewColumnSortMode.NotSortable;

            HeaderCheckBox = new CheckBox();
            HeaderCheckBox.Size = new Size(15, 15);
            dataGridView1.Controls.Add(HeaderCheckBox);

            HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
            dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
            //for (int i = 0; i < itemName.GetLength(0); i++)
            //{
            //    DataRow row = dt.NewRow();
            //    row[0] = true;
            //    row[1] = itemName[i, 0];
            //    row[2] = itemName[i, 1];
            //    row[3] = itemName[i, 2];
            //    row[4] = "";
            //    row[5] = "";
            //    dt.Rows.Add(row);
            //}
            dataGridView1.Columns[0].Width = 70;
            oRectangle = this.dataGridView1.GetCellDisplayRectangle(0, -1, true);
            Color color = dataGridView1.Columns[0].HeaderCell.Style.BackColor;
            Point oPoint = new Point();
            oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;// 3;
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;
            HeaderCheckBox.Location = oPoint;
            HeaderCheckBox.BackColor = color;
            HeaderCheckBox.Checked = true;
            dataGridView1.Columns[0].HeaderText = "";
        }

        public void InitGroupGrid(string groupid)
        {
            dt = new System.Data.DataTable();
            //DataColumn col1 = new DataColumn("测试项目");
            //DataColumn col6 = new DataColumn("单位");
            //DataColumn col2 = new DataColumn("指标要求");
            //DataColumn col3 = new DataColumn("测试值");
            //DataColumn col4 = new DataColumn("测试结果");//,System.Type.GetType("System.Byte[]"));
            //DataColumn col5 = new DataColumn("分类");
            //DataColumn col = new DataColumn("全选");
            //col.DataType = typeof(Boolean);
            //dt.Columns.Add(col);
            //dt.Columns.Add(col5);
            //dt.Columns.Add(col1);
            //dt.Columns.Add(col6);
            //dt.Columns.Add(col2);
            //dt.Columns.Add(col3);
            //dt.Columns.Add(col4);
            TestRecordManage trm = new TestRecordManage();
            trm.OpenDataBase();

            dt.Columns.AddRange(new DataColumn[] { new DataColumn("全选", typeof(Boolean)) });
            dt.Columns[0].DefaultValue = true;
            StringBuilder sb = new StringBuilder();
            sb.Append("select distinct TestGroup as N'分类',SUBSTRING(TestName, 1, case  when charindex('#',TestName )=0 then len(TestName) ");
            sb.Append(" else charindex('#',TestName )-1 end ) AS N'测试项目',unit AS N'单位',remark AS N'指标要求','' AS N'测试值',''AS N'测试结果', ");
            sb.Append("ttic.TestGroupID,ttic.TestID from tb_TestIndexConfig ttic where  ttic.TestGroupID in (" + groupid + ") order by  ttic.TestGroupID,ttic.TestID");
            dt.Merge(trm.ExecuteSqlTable(sb.ToString()));
            dt.Columns.Remove(dt.Columns["TestGroupID"]);
            dt.Columns.Remove(dt.Columns["TestID"]);

            trm.CloseDataBase();






            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].FillWeight = 45;
            dataGridView1.Columns[2].FillWeight = 220;
            dataGridView1.Columns[0].ValueType = typeof(bool);
            dataGridView1.Columns[0].ReadOnly = false;
            for (int i = 1; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].ReadOnly = true;
            }
            dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[5].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[6].SortMode = DataGridViewColumnSortMode.NotSortable;

            HeaderCheckBox = new CheckBox();
            HeaderCheckBox.Size = new Size(15, 15);
            dataGridView1.Controls.Add(HeaderCheckBox);

            HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
            dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
            //for (int i = 0; i < itemName.GetLength(0); i++)
            //{
            //    DataRow row = dt.NewRow();
            //    row[0] = true;
            //    row[1] = itemName[i, 0];
            //    row[2] = itemName[i, 1];
            //    row[3] = itemName[i, 2];
            //    row[4] = itemName[i, 3];
            //    row[5] = "";
            //    row[6] = "";
            //    dt.Rows.Add(row);
            //}
            dataGridView1.Columns[0].Width = 70;
            oRectangle = this.dataGridView1.GetCellDisplayRectangle(0, -1, true);
            Color color = dataGridView1.Columns[0].HeaderCell.Style.BackColor;
            Point oPoint = new Point();
            oPoint.X = oRectangle.Location.X + (oRectangle.Width - HeaderCheckBox.Width) / 2 + 1;// 3;
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - HeaderCheckBox.Height) / 2 + 1;
            HeaderCheckBox.Location = oPoint;
            HeaderCheckBox.BackColor = color;
            HeaderCheckBox.Checked = true;
            dataGridView1.Columns[0].HeaderText = "";
        }

        private void dgvSelectAll_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }

        private void HeaderCheckBox_MouseClick(object sender, MouseEventArgs e)
        {
            HeaderCheckBoxClick((CheckBox)sender);
        }

        private void HeaderCheckBoxClick(CheckBox HCheckBox)
        {
            foreach (DataGridViewRow Row in dataGridView1.Rows)
            {
                ((DataGridViewCheckBoxCell)Row.Cells["全选"]).Value = HCheckBox.Checked;
            }
            dataGridView1.RefreshEdit();
        }

        public void LoadGSMGrid(string groupid)
        {
            dt = new System.Data.DataTable();

            //DataColumn col2 = new DataColumn("测试项目");
            //DataColumn col3 = new DataColumn("指标要求");
            //DataColumn col4 = new DataColumn("测试值");
            //DataColumn col5 = new DataColumn("测试结果");
            //dt.Columns.Add(col2);
            //dt.Columns.Add(col3);
            //dt.Columns.Add(col4);
            //dt.Columns.Add(col5);

            TestRecordManage trm = new TestRecordManage();
            trm.OpenDataBase();
            StringBuilder sb = new StringBuilder();
            sb.Append("select distinct  SUBSTRING(TestName, 1, case  when charindex('#',TestName )=0 then len(TestName) ");
            sb.Append(" else charindex('#',TestName )-1 end ) AS N'测试项目',remark AS N'指标要求','' AS N'测试值',''AS N'测试结果', ");
            sb.Append("ttic.TestGroupID,ttic.TestID from tb_TestIndexConfig ttic where  ttic.TestGroupID in (" + groupid + ") order by  ttic.TestGroupID,ttic.TestID");
            dt.Merge(trm.ExecuteSqlTable(sb.ToString()));
            dt.Columns.Remove(dt.Columns["TestGroupID"]);
            dt.Columns.Remove(dt.Columns["TestID"]);

            trm.CloseDataBase();

            
            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.ReadOnly = true;
            //for (int i = 0; i < itemName.GetLength(0); i++)
            //{
            //    DataRow row = dt.NewRow();
            //    row[0] = itemName[i, 0];
            //    row[1] = itemName[i, 1];
            //    row[2] = "";
            //    row[3] = "";
            //    dt.Rows.Add(row);
            //}
        }

        public void LoadFunTestGrid(string groupid)
        {
            dt = new System.Data.DataTable();
            //DataColumn col = new DataColumn("试验项目");
            //DataColumn col2 = new DataColumn("试验结果");
            //dt.Columns.Add(col);
            //dt.Columns.Add(col2);

            TestRecordManage trm = new TestRecordManage();
            trm.OpenDataBase();
            StringBuilder sb = new StringBuilder();
            sb.Append("select distinct  SUBSTRING(TestName, 1, case  when charindex('#',TestName )=0 then len(TestName) ");
            sb.Append(" else charindex('#',TestName )-1 end ) AS N'测试项目',''AS N'测试结果', ");
            sb.Append("ttic.TestGroupID,ttic.TestID from tb_TestIndexConfig ttic where  ttic.TestGroupID in (" + groupid + ") order by  ttic.TestGroupID,ttic.TestID");
            dt.Merge(trm.ExecuteSqlTable(sb.ToString()));
            dt.Columns.Remove(dt.Columns["TestGroupID"]);
            dt.Columns.Remove(dt.Columns["TestID"]);

            trm.CloseDataBase();

            dataGridView1.DataSource = dt;
            dataGridView1.ReadOnly = true;
            dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
            //for (int i = 0; i < item.Length; i++)
            //{
            //    DataRow row = dt.NewRow();
            //    row[0] = item[i];
            //    row[1] = "";
            //    dt.Rows.Add(row);
            //}
        }
        public void InitHeLUQiGrid(string groupid)
        {
            dt = new System.Data.DataTable();
           
            
            //DataColumn col1 = new DataColumn("测试项目");
            //DataColumn col2 = new DataColumn("指标要求");
            //DataColumn col3 = new DataColumn("测试值");
            //DataColumn col4 = new DataColumn("测试结果");//,System.Type.GetType("System.Byte[]"));
            //DataColumn col = new DataColumn("选择测试");
            //DataColumn col5 = new DataColumn("取标准值");
            //DataColumn col6 = new DataColumn("组属性");
            //col.DataType = typeof(Boolean);
            //col5.DataType = typeof(Boolean);
            //dt.Columns.Add(col);
            //dt.Columns.Add(col5);
            //dt.Columns.Add(col6);
            //dt.Columns.Add(col1);
            //dt.Columns.Add(col2);
            //dt.Columns.Add(col3);
            //dt.Columns.Add(col4);

            TestRecordManage trm = new TestRecordManage();
            trm.OpenDataBase();

            dt.Columns.AddRange(new DataColumn[] { new DataColumn("选择测试", typeof(Boolean)) });
            dt.Columns[0].DefaultValue = true;
            dt.Columns.AddRange(new DataColumn[] { new DataColumn("取标准值", typeof(Boolean)) });
            dt.Columns[1].DefaultValue = true;
            StringBuilder sb = new StringBuilder();
            sb.Append("select distinct TestGroup as N'分类',SUBSTRING(TestName, 1, case  when charindex('#',TestName )=0 then len(TestName) ");
            sb.Append(" else charindex('#',TestName )-1 end ) AS N'测试项目',remark AS N'指标要求','' AS N'测试值',''AS N'测试结果', ");
            sb.Append("ttic.TestGroupID,ttic.TestID from tb_TestIndexConfig ttic where  ttic.TestGroupID in (" + groupid + ") order by  ttic.TestGroupID,ttic.TestID");
            dt.Merge(trm.ExecuteSqlTable(sb.ToString()));
            dt.Columns.Remove(dt.Columns["TestGroupID"]);
            dt.Columns.Remove(dt.Columns["TestID"]);

            trm.CloseDataBase();



            dataGridView1.DataSource = dt;
            dataGridView1.Columns[0].FillWeight = 45;
            dataGridView1.Columns[0].ValueType = typeof(bool);
            dataGridView1.Columns[0].ReadOnly = false;
            dataGridView1.Columns[1].FillWeight = 45;
            dataGridView1.Columns[1].ValueType = typeof(bool);
   
            Xml_HeluQi xmlhlq = new Xml_HeluQi();
            if (xmlhlq.Xmlexist)//如果没有过初始值那么用户必选进行初始化
            {
               dataGridView1.Columns[1].ReadOnly = false;
            }
            else
            {
                xmlhlq.WriteXML("","", "");
                dataGridView1.Columns[1].ReadOnly = true;
            }
            for (int i = 2; i < dataGridView1.Columns.Count; i++)
            {
                dataGridView1.Columns[i].ReadOnly = true;
            }
            dataGridView1.Columns[0].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[1].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[2].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[3].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[4].SortMode = DataGridViewColumnSortMode.NotSortable;
            dataGridView1.Columns[5].SortMode = DataGridViewColumnSortMode.NotSortable;

            //HeaderCheckBox = new CheckBox();
            //HeaderCheckBox.Size = new Size(15, 15);
            //dataGridView1.Controls.Add(HeaderCheckBox);

           //HeaderCheckBox.MouseClick += new MouseEventHandler(HeaderCheckBox_MouseClick);
            dataGridView1.CurrentCellDirtyStateChanged += new EventHandler(dgvSelectAll_CurrentCellDirtyStateChanged);
            //for (int i = 0; i < itemName.GetLength(0); i++)
            //{
            //    DataRow row = dt.NewRow();
            //    row[0] = true;
            //    row[1] = true;
            //    row[2] = itemName[i, 0]; 
            //    row[3] = itemName[i, 1];
            //    row[4] = itemName[i, 2];
            //    row[5] = "";
            //    row[6] = "";
            //    dt.Rows.Add(row);
            //} 
        }
        public void ClearDataGridView(string formName, DataGridView dgv)
        {
            if (formName == "450MHz调度命令功能试验")
            {
                InitDataGridView(true, dgv);
            }
            if (formName == "发射机性能指标测试")
            {
                InitDataGridView(false, dgv);
            }
            if (formName == "同频接收机性能指标测试")
            {
                InitDataGridView(false, dgv);
            }
            if (formName == "异频接收机性能指标测试")
            {
                InitDataGridView(false, dgv);
            }
            if (formName == "信令指标测试")
            {
                InitDataGridView(false, dgv);
            }
            if (formName == "GSM-R语音模块电性能测试")
            {
                InitDataGridView(false, dgv);
            }
            if (formName == "GSM-R数据模块电性能测试")
            {
                InitDataGridView(false, dgv);
            }
            if (formName == "GSM-R数据模块功能试验")
            {
                InitDataGridView(true, dgv);
            }
            if (formName == "450MHz列尾功能试验")
            {
                InitDataGridView(true, dgv);
            }
            if (formName == "LBJ功能试验")
            {
                InitDataGridView(true, dgv);
            }
            if (formName == "列车接近预警功能试验")
            {
                InitDataGridView(true, dgv);
            }
            if (formName == "GSM-R语音模块单板电性能测试")
            {
                InitDataGridView(false, dgv);
            }
            if (formName == "GSM-R数据模块单板电性能测试")
            {
                InitDataGridView(false, dgv);
            }
            if (formName == "LBJ发射机电性能测试")
            {
                InitDataGridView(false, dgv);
            }
            if (formName == "LBJ接收机电性能测试")
            {
                InitDataGridView(false, dgv);
            }
        }

        public void CloseSerialPort()
        {
            //if (GsmDigTest != null || GsmVoTest != null)
            //    GsmDigTest.OSP_2.Close();
        }

        private void InitDataGridView(bool isFunTrial, DataGridView dgv)
        {
            if (isFunTrial)
            {
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    dgv.Rows[i].Cells["试验结果"].Value = "";
                }
            }
            else
            {
                for (int i = 0; i < dgv.Rows.Count; i++)
                {
                    dgv.Rows[i].Cells["测试值"].Value = "";
                    dgv.Rows[i].Cells["测试结果"].Value = "";
                }
            }
        }

        private void InitDataGridView(bool isFunTrial)
        {
            if (isFunTrial)
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {

                    dataGridView1.Rows[i].Cells["试验结果"].Value = "";

                }
            }
            else
            {
                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (dataGridView1[0, i].Value.ToString() == "True")
                    {
                        dataGridView1.Rows[i].Cells["测试值"].Value = "";
                        dataGridView1.Rows[i].Cells["测试结果"].Value = "";
                    }
                }
            }
        }

        public void ClearSource()
        {
            if (GsmVoTest != null)
            {
                GsmVoTest.ClearSource();
            }
            if (GsmDigTest != null)
            {
                GsmDigTest.ClearSource();
            }
            if (LbjTranTest != null)
            {
                LbjTranTest.ClearSoure();
            }
            if (LbjRecTest != null)
            {
                LbjRecTest.ClearSoure();
            }
            if (GsmFunTrial != null)
            {
                GsmFunTrial.ClearSource();
            }
        }

        #region 自定义单元格合并方法
        /// <summary>
        /// 画单元格
        /// </summary>
        /// <param name="e"></param>
        ///  <param mergerColumnIndex>要合并的列的索引号</param>
        private void DrawCell(DataGridViewCellPaintingEventArgs e, DataGridView dgv, int mergerColumnIndex)
        {
            if (e.CellStyle.Alignment == DataGridViewContentAlignment.NotSet)
            {
                e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }
            Brush gridBrush = new SolidBrush(dgv.GridColor);
            SolidBrush backBrush = new SolidBrush(Color.White);
            SolidBrush backBrush2 = new SolidBrush(e.CellStyle.BackColor);
            SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
            int cellwidth;
            int cellheight;
            int offset = 0;
            //上面相同的行数
            int UpRows = 0;
            //下面相同的行数
            int DownRows = 0;
            //总行数
            //int mergerColumnIndex = 0; // 要合并的列的索引号
            int count = 0;
            //if (dgv.Name == "dataGridView1")
            //{
            //    mergerColumnIndex = 1;
            //}
            //else
            //{
            //    mergerColumnIndex = 0;
            //}
            if (e.RowIndex != -1)
            {
                cellwidth = e.CellBounds.Width;
                cellheight = e.CellBounds.Height;
                Pen gridLinePen = new Pen(gridBrush);
                string curValue = e.Value == null ? "" : e.Value.ToString().Trim();
                string curSelected = dgv.CurrentRow.Cells[mergerColumnIndex].Value == null ? "" : dgv.CurrentRow.Cells[mergerColumnIndex].Value.ToString().Trim();
                if (!string.IsNullOrEmpty(curValue))
                {
                    #region 获取下面的行数
                    for (int i = e.RowIndex; i < dgv.Rows.Count; i++)
                    {
                        if (dgv.Rows[i].Cells[mergerColumnIndex].Value.ToString().Equals(curValue) && e.ColumnIndex == mergerColumnIndex)
                        {
                            dgv.Rows[i].Cells[mergerColumnIndex].Selected = dgv.Rows[e.RowIndex].Cells[mergerColumnIndex].Selected;
                            DownRows++;
                            if (e.RowIndex != i)
                            {
                                cellwidth = cellwidth < dgv.Rows[i].Cells[mergerColumnIndex].Size.Width ? cellwidth : dgv.Rows[i].Cells[mergerColumnIndex].Size.Width;
                                cellheight = Math.Min(cellheight, dgv.Rows[i].Cells[mergerColumnIndex].Size.Height);

                                // offset  += Math.Abs(cellheight - dgv.Rows[i].Cells[e.ColumnIndex].Size.Height);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    #endregion

                    #region 获取上面的行数
                    for (int i = e.RowIndex; i >= 0; i--)
                    {
                        if (dgv.Rows[i].Cells[mergerColumnIndex].Value.ToString().Equals(curValue) && e.ColumnIndex == mergerColumnIndex)
                        {
                            dgv.Rows[i].Cells[mergerColumnIndex].Selected = dgv.Rows[e.RowIndex].Cells[mergerColumnIndex].Selected;
                            UpRows++;
                            if (e.RowIndex != i)
                            {
                                cellwidth = cellwidth < dgv.Rows[i].Cells[mergerColumnIndex].Size.Width ? cellwidth : dgv.Rows[i].Cells[mergerColumnIndex].Size.Width;
                                cellheight = Math.Min(cellheight, dgv.Rows[i].Cells[mergerColumnIndex].Size.Height);
                                offset += Math.Abs(cellheight - dgv.Rows[i].Cells[mergerColumnIndex].Size.Height);
                            }
                        }
                        else
                        {
                            break;
                        }
                    }
                    #endregion
                    count = DownRows + UpRows - 1;
                    if (count < 2)
                    {
                        return;
                    }

                }
                if (dgv.Rows[e.RowIndex].Selected)
                {
                    backBrush.Color = e.CellStyle.SelectionBackColor;
                    fontBrush.Color = e.CellStyle.SelectionForeColor;
                }
                //以背景色填充

                if (dgv.Columns[mergerColumnIndex].Name == "测试值" || dgv.Columns[mergerColumnIndex].Name == "测试结果" || dgv.Columns[mergerColumnIndex].Name == "试验结果")
                    e.Graphics.FillRectangle(backBrush2, e.CellBounds);
                else
                {
                    if (e.State == DataGridViewElementStates.Visible)
                    {
                        backBrush = new SolidBrush(Color.OliveDrab);
                        e.Graphics.FillRectangle(backBrush, e.CellBounds);
                    }
                    else
                    {
                        e.Graphics.FillRectangle(backBrush, e.CellBounds);
                    }
                }
                if (dgv.Rows[e.RowIndex].Cells[mergerColumnIndex].Value.ToString() == "")
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                //画字符串
                PaintingFont(e, cellwidth, UpRows, DownRows, count, cellheight, offset);
                // dgv.Rows[e.RowIndex].Cells[mergerColumnIndex].Value = "";
                if (DownRows == 1)
                {
                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                    count = 0;
                }
                //// 画右边线
                //if (dgv.Rows[e.RowIndex].Cells[mergerColumnIndex].Value.ToString() != "")
                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);

                e.Handled = true;
                dgv.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            }
        }
        /// <summary>
        /// 画字符串
        /// </summary>
        /// <param name="e"></param>
        /// <param name="cellwidth"></param>
        /// <param name="UpRows"></param>
        /// <param name="DownRows"></param>
        /// <param name="count"></param>
        private void PaintingFont(System.Windows.Forms.DataGridViewCellPaintingEventArgs e, int cellwidth, int UpRows, int DownRows, int count, int cellheight, int offset)
        {
            SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor);
            int fontheight = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Height;
            int fontwidth = (int)e.Graphics.MeasureString(e.Value.ToString(), e.CellStyle.Font).Width;
            // int cellheight = cellheight;
            if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomCenter)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y + cellheight * DownRows - fontheight, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomLeft)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X, e.CellBounds.Y + cellheight * DownRows - fontheight, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.BottomRight)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y + cellheight * DownRows - fontheight, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleCenter)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleLeft)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2 - offset, e.CellBounds.Width, e.CellBounds.Height);//
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.MiddleRight)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopCenter)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1), e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopLeft)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X, e.CellBounds.Y - cellheight * (UpRows - 1), e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else if (e.CellStyle.Alignment == DataGridViewContentAlignment.TopRight)
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + cellwidth - fontwidth, e.CellBounds.Y - cellheight * (UpRows - 1), e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
            else
            {
                System.Drawing.Rectangle rec = new System.Drawing.Rectangle(e.CellBounds.X + (cellwidth - fontwidth) / 2, e.CellBounds.Y - cellheight * (UpRows - 1) + (cellheight * count - fontheight) / 2, e.CellBounds.Width, e.CellBounds.Height);
                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font, fontBrush, rec);
            }
        }
        #endregion

        private void SingleModFrm_FormClosing(object sender, FormClosingEventArgs e)
        {
            MessageBox.Show("窗口已退出");
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (MainFrm.MotherFrm.formName == "LBJ发射机电性能测试" || MainFrm.MotherFrm.formName == "信令指标测试")
            {
                DataGridView dataGridView = sender as DataGridView;
                if (e.RowIndex > -1 && e.ColumnIndex == 1)
                {
                    DrawCell(e, dataGridView,1);
                }
            }
            if (MainFrm.MotherFrm.formName == "合路器测试")
            {
                DataGridView dataGridView = sender as DataGridView;
                  if (e.RowIndex > -1 && e.ColumnIndex == 2)
                {
                    DrawCell(e, dataGridView,2);
                }
            }
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (MainFrm.MotherFrm.formName == "信令指标测试")
            {
                DataGridView dataGridView = sender as DataGridView;
                if (e.RowIndex != -1 && e.ColumnIndex == 0)
                {
                    if (e.RowIndex == 0 || e.RowIndex == 8)
                    {
                        if (e.RowIndex == 0)
                        {
                            dataGridView.Rows[8].Cells[0].Value = ((bool)dataGridView.Rows[0].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[0].Cells[0].Value = ((bool)dataGridView.Rows[8].Cells[0].Value);
                        }
                    }
                    if (e.RowIndex == 1 || e.RowIndex == 9)
                    {
                        if (e.RowIndex == 1)
                        {
                            dataGridView.Rows[9].Cells[0].Value = ((bool)dataGridView.Rows[1].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[1].Cells[0].Value = ((bool)dataGridView.Rows[9].Cells[0].Value);
                        }
                    }
                    if (e.RowIndex == 2 || e.RowIndex == 10)
                    {
                        if (e.RowIndex == 2)
                        {
                            dataGridView.Rows[10].Cells[0].Value = ((bool)dataGridView.Rows[2].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[2].Cells[0].Value = ((bool)dataGridView.Rows[10].Cells[0].Value);
                        }
                    }
                    if (e.RowIndex == 3 || e.RowIndex == 11)
                    {
                        if (e.RowIndex == 3)
                        {
                            dataGridView.Rows[11].Cells[0].Value = ((bool)dataGridView.Rows[3].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[3].Cells[0].Value = ((bool)dataGridView.Rows[11].Cells[0].Value);
                        }
                    }
                    if (e.RowIndex == 4 || e.RowIndex == 12)
                    {
                        if (e.RowIndex == 4)
                        {
                            dataGridView.Rows[12].Cells[0].Value = ((bool)dataGridView.Rows[4].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[4].Cells[0].Value = ((bool)dataGridView.Rows[12].Cells[0].Value);
                        }
                    }
                    if (e.RowIndex == 5 || e.RowIndex == 13)
                    {
                        if (e.RowIndex == 5)
                        {
                            dataGridView.Rows[13].Cells[0].Value = ((bool)dataGridView.Rows[5].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[5].Cells[0].Value = ((bool)dataGridView.Rows[13].Cells[0].Value);
                        }
                    }
                    if (e.RowIndex == 6 || e.RowIndex == 14)
                    {
                        if (e.RowIndex == 6)
                        {
                            dataGridView.Rows[14].Cells[0].Value = ((bool)dataGridView.Rows[6].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[6].Cells[0].Value = ((bool)dataGridView.Rows[14].Cells[0].Value);
                        }
                    }
                    if (e.RowIndex == 7 || e.RowIndex == 15)
                    {
                        if (e.RowIndex == 7)
                        {
                            dataGridView.Rows[15].Cells[0].Value = ((bool)dataGridView.Rows[7].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[7].Cells[0].Value = ((bool)dataGridView.Rows[15].Cells[0].Value);
                        }
                    }
                }
            }
            if (MainFrm.MotherFrm.formName == "异频接收机性能指标测试")
            {
                DataGridView dataGridView = sender as DataGridView;
                if (e.RowIndex != -1 && e.ColumnIndex == 0)
                {
                    if (e.RowIndex == 0 || e.RowIndex == 1)
                    {
                        if (e.RowIndex == 0)
                        {
                            dataGridView.Rows[1].Cells[0].Value = ((bool)dataGridView.Rows[0].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[0].Cells[0].Value = ((bool)dataGridView.Rows[1].Cells[0].Value);
                        }
                    }
                    if (e.RowIndex == 6 || e.RowIndex == 7)
                    {
                        if (e.RowIndex == 7)
                        {
                            dataGridView.Rows[6].Cells[0].Value = ((bool)dataGridView.Rows[7].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[7].Cells[0].Value = ((bool)dataGridView.Rows[6].Cells[0].Value);
                        }
                    }
                }

            }
            if (MainFrm.MotherFrm.formName == "同频接收机性能指标测试")
            {
                DataGridView dataGridView = sender as DataGridView;
                if (e.RowIndex != -1 && e.ColumnIndex == 0)
                {
                    if (e.RowIndex == 5 || e.RowIndex == 6)
                    {
                        if (e.RowIndex == 6)
                        {
                            dataGridView.Rows[5].Cells[0].Value = ((bool)dataGridView.Rows[6].Cells[0].Value);

                        }
                        else
                        {
                            dataGridView.Rows[6].Cells[0].Value = ((bool)dataGridView.Rows[5].Cells[0].Value);
                        }
                    }
                }
            }
        }
    }
}
